export const pieOption = (data , formatter , opt) => {
  return {
      ...opt,
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c} ({d}%)',
        backgroundColor: "rgba(255,255,255,0.75)",
        textStyle: {
          color: '#000'
        }
      },
      legend: {
        orient: 'vertical',
        left: 0,
        top: 'center',
        data: data.legend,
        icon: 'circle',
        formatter: formatter || function(name){
          return `${name}  {value|${data.legendDatas[name]['value']}, ${data.legendDatas[name]['pre']}%}`;
        },
        textStyle: {
          rich: {
            value: {
              color: '#999'
            }
          }
        }
      },
      series: data.series ? data.series : [
        {
          type: 'pie',
          radius: ['40%' , '60%'],
          center: ['75%' , '50%'],
          label: {
            show: false
          },
          labelLine: {
            show: false
          },
          data: data.data
        }
      ]
  }
}

export const getDataCollectCountStr = (num , unit) => {
  unit = unit || '条';
  if(num > 99999999){
    return (num / 100000000).toFixed(2) + '亿' + unit;
  }else if(num < 100000000 && num > 9999){
    return (num / 100000000).toFixed(2) + '万' + unit;
  }else{
    return num + unit;
  }
}

export const createTestData = (legends) => {
  let data = [];
  legends.forEach(
    item => {
      data.push({name: item , value: Math.ceil(Math.random() * 10000)})
    }
  )
  return data;
}

export const getPre = (count , values) => {
  Object.keys(values).forEach(
    key => {
      let value = values[key].c;
      let pre = (value / count * 100);
      values[key]['pre'] = Number(pre.toString().match(/^\d+(?:\.\d{0,2})?/));
    }
  )
  return values;
}